# Author: Yu Bin
# Date: 2024/12/27 19:05:18
# E-mail: 853823259@qq.com

# 1.把今天讲的元组，字典，字符串，集合，函数缺省参数等所有接口基本功能练习一遍，和上课代码保持一致
# 元组
info_tuple = ("zhangsan", 18, 1.75, "zhangsan")
print(info_tuple[0])
print(info_tuple.index("zhangsan"))
print(info_tuple.count("zhangsan"))
print(len(info_tuple))

info_tuple = ("zhangsan", 18, 1.75)
for my_info in info_tuple:
    print(my_info)

info_tuple = ("小明", 21, 1.85)
print("%s 年龄是 %d 身高是 %.2f" % info_tuple)
info_str = "%s 年龄是 %d 身高是 %.2f" % info_tuple
print(info_str)

# 字典
xiaoming = {"name": "小明",
            "age": 18,
            "gender": True,
            "height": 1.75}
for k in xiaoming:
    print("%s: %s" % (k, xiaoming[k]))

xiaoming_dict = {"name": "小明"}
# 取值
print(xiaoming_dict["name"])
# 增加/修改
xiaoming_dict["age"] = 18
xiaoming_dict["name"] = "小小明"
# 删除
xiaoming_dict.pop("name")
print(xiaoming_dict)

xiaoming_dict = {"name": "小明",
                 "age": 18}
# 统计键值对数量
print(len(xiaoming_dict))
# 合并字典
temp_dict = {"height": 1.75,
             "age": 20}
xiaoming_dict.update(temp_dict)
# 清空字典
xiaoming_dict.clear()
print(xiaoming_dict)
xiaoming_dict = {"name": "小明",
                 "qq": "123456",
                 "phone": "10086"}
# 迭代遍历字典
for k in xiaoming_dict:
    print("%s - %s" % (k, xiaoming_dict[k]))

# 字符串
num_str = "0123456789"
# 1. 截取从 2 ~ 5 位置 的字符串
print(num_str[2:6])
# 2. 截取从 2 ~ ‘末尾’的字符串
print(num_str[2:])
# 3. 截取从 ‘开始’ ~ 5 位置的字符串
print(num_str[:6])
# 4. 截取完整的字符串
print(num_str[:])
# 5. 从开始位置，每隔一个字符截取字符串
print(num_str[::2])
# 6. 从索引 1 开始， 每隔一个取一个
print(num_str[1::2])
# 倒序切片
print(num_str[-1])
# 7. 截取从 2 ~ `末尾 - 1` 的字符串
print(num_str[2:-1])
# 8. 截取字符串末尾两个字符
print(num_str[-2:])
# 9. 字符串的逆序
print(num_str[::-1])

# 集合
fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)
x = fruits.copy()
print(x)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.difference_update(y)
print(x)
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits)


# 函数缺省参数
def print_info(name, gender=True):
    gender_text = "男生"
    if not gender:
        gender_text = "女生"
    print("%s 是 %s" % (name, gender_text))


print_info("xiaoming")
